// If you want to prevent dragging, uncomment this section
function preventBehavior(e) {
    e.preventDefault();
};
document.addEventListener("touchmove", preventBehavior, false);

/* USED TO DEBUG AND CHECK THE INITIALIZATION OF PHONEGAP PLUGINS
function onBodyLoad() {
   // document.addEventListener("deviceready", onDeviceReady, false);
}

/function onDeviceReady() {
    window.plugins.social.available(function(avail) {
        if (avail) {
            //alert("Social Found");
        } else {
            alert("Could not find Social");
        }
    });
    //var db = window.openDatabase("Database", "1.0", "Feed", 200000);
    
}*/

// Script runs main() when when PhoneGap is fully loaded.
// http://docs.phonegap.com/en/1.4.1/phonegap_events_events.md.html#deviceready
function init() {
  document.addEventListener("deviceready", main, true);
  $(document).ready(main);
}

// process the confirmation dialog result
function onConfirm(buttonIndex) {
  navigator.splashscreen.hide();
}

function splashscreenShow(){
  navigator.splashscreen.show();
}


function splashscreenHide(){
  setTimeout(function() {
    navigator.splashscreen.hide();
  }, 3000);
}
// Show a custom confirmation dialog
function showConfirm() {
  
  navigator.notification.confirm(
            'No se detecto contectividad, las imagens y algunos recursos no podran ser visualizados',  // message
            onConfirm,              // callback to invoke with index of button pressed
            'Advertencia',            // title
            'Aceptar'          // buttonLabels
        );
    }



function share(url){
    url = 'http://www.lasillavacia.com/' + url;
    window.plugins.social.share('La Silla Vacia', url, 'www/img/logo.png');
}

function shareTitle(title, url){
    title = title.trim();
    url = url.trim();
    url = 'http://www.lasillavacia.com/' + url;
    window.plugins.social.share(title + ' - ' + url, '', url);
}

function clickAd(uid){
    window.plugins.social.click(uid);
}

function viewAd(uid){
    window.plugins.social.view(uid);
}

function success(result) {
    alert(result);
}

function failure(result) {
    alert(result);
}


//Abrir en Browser Externo    
document.addEventListener('click', function(e) {
    if (e.hasOwnProperty('target')){
        if ((e.srcElement.target === "" || e.srcElement.target === "_blank") && e.srcElement.href.indexOf("#phonegap=external") === -1) {
            if(e.srcElement.href.indexOf("La%20Silla") == -1){
                e.preventDefault();
                e.stopPropagation();
                window.open(e.srcElement.href, '_blank', 'location=yes');
            }
        }
    }    
}, true);

//Add Event Handler
function addEventHandler(elem,eventType,handler) {
   if (elem.addEventListener)
       elem.addEventListener (eventType,handler,false);
   else if (elem.attachEvent)
       elem.attachEvent ('on'+eventType,handler); 
}


function horasDiferencia(d1, d2) {
    var t2 = d2.getTime();
    var t1 = d1.getTime();
    var dif=parseInt((t2-t1)/(3600*1000));
    var dia=1;
    if(dif>=24){
        return Math.floor(dia =dif/24)+"d";

    }else{
        return dif+"h";
    }
}

function hide(){
    var idfeed=this.id;
    idfeed=idfeed.substr(4,4);

    var item = document.getElementById('columns');
    item.className=(item.className=='hidden')?'unhidden':'hidden';

    var item = document.getElementById('columns-nota');
    item.className=(item.className=='hidden')?'unhidden':'hidden';



    var nota = document.getElementById('feed-nota'+idfeed);
    nota.className=(nota.className=='hidden')?'unhidden':'hidden';

    var item = document.getElementById('wrapper');
    item.className=(item.className=='hidden')?'unhidden':'hidden';
}

function open_win(dir)
{
    var ref = window.open(dir, '_blank', 'EnableViewPortScale=yes');
    ref.close();
}

function open_win_mini(dir)
{
    dir = encodeURI(dir.trim());
    var ref = window.open(dir, '_blank', 'EnableViewPortScale=yes');
    ref.close();
}

//taken from Twitter's blogger.js
function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);
 
  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}





